package com.hiwetop.cp;

import com.fj.hiwetoptools.RandomGenerator;
import com.fj.hiwetoptools.StringUtil;
import com.fj.hiwetoptools.http.HttpConnection;
import com.fj.hiwetoptools.http.HttpRequest;
import com.fj.hiwetoptools.http.HttpUtil;
import com.fj.hiwetoptools.http.Method;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by linyu on 2016/12/12.
 */
public class Night0591 {
    public static List<Detail> lists = Lists.newArrayList();
    public static final String[] replys = new String[]{"我是来混积分的，请直接无视我就好了",
    "感谢楼主分享","感谢楼主帖子","抖一抖，积分到手","有什么号码推荐","现分享感受最好的一次体验",
            "抖抖积分到手","这种资源求老手分享","老司机，带带我","在哪里啊，资源共享啊，lang友"};

    static String loginUrl = "http://www.0591night.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
    static String list = "http://www.0591night.com/forum.php?mod=forumdisplay&fid=38&page=1";
    //static String detail = "http://www.0591night.com/forum.php?mod=viewthread&tid=955&extra=page%3D1";
    static String replyParam = "&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1";
    public static HttpUrlConnectionExample http = new HttpUrlConnectionExample();

    public static void main(String[] args) throws Exception {


        // make sure cookies is turn on
        CookieHandler.setDefault(new CookieManager());

        doLogin();
        list();
        doDetail();


    }

    public static void doLogin()throws  Exception{
        http.GetPageContent("http://www.0591night.com/member.php");
        // 1. Send a "GET" request, so that you can extract the form's data.
        String postParams = "fastloginfield=username&username=nimabibi&password=nimabibi163&quickforward=yes&handlekey=ls";

        // 2. Construct above post's content and then send a POST request for
        // authentication
        http.sendPost(loginUrl, postParams,"http://www.0591night.com/forum.php");

        TimeUnit.SECONDS.sleep(new RandomGenerator().nextNumber(2,6));
    }

    public static void list()throws  Exception{
        String result = http.GetPageContent(list);
        String page = URIUtils.getParameter(list,"page");
        Document doc = Jsoup.parse(result);
        Elements elementsByAttribute = doc.select(".xst");
        for (Element el: elementsByAttribute) {
            System.out.println(el.attr("href"));
            System.out.println(el.text());
            String url = el.attr("href");
            String id = URIUtils.getParameter(url,"tid");
            String replyUrl = url + replyParam;
            if (id.equals("745")){
                continue;
            }
            Detail d = new Detail(Ints.tryParse(id),url,el.text(),replyUrl,page);
            lists.add(d);
        }
        TimeUnit.SECONDS.sleep(new RandomGenerator().nextNumber(2,6));
    }

    public static void doDetail() throws Exception{
        for (Detail d: lists) {
            reply(d);
        }
    }

    public static void reply(Detail detail)  throws Exception{
        System.out.println("reply:"+detail.title + "-" + detail.replyUrl);
        String re = http.GetPageContent(detail.url);
        Document doc =  Jsoup.parse(re);
        Element fastpostmessage = doc.getElementById("fastpostmessage");
        Element posttime = doc.getElementById("posttime");
        Element formhash = doc.select("input[name=formhash]").get(0);
        String postParams = "message={}&posttime={}&formhash={}&usesig=&subject=++";
        String message = URLEncoder.encode(replys[new RandomGenerator().nextNumber(1,9)],"gbk");
        postParams = StringUtil.format(postParams,message,posttime.val(),formhash.val());
        TimeUnit.SECONDS.sleep(new RandomGenerator().nextNumber(2,6));
        http.sendPost(detail.replyUrl, postParams,detail.url);
    }
}
